package me.xue.communityhealth.admin.controller;

import me.xue.communityhealth.util.ApplicationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Paths;

@RestController
public class DrugPhotoController {

    private final ResourceLoader resourceLoader;

    @Autowired
    public DrugPhotoController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @GetMapping("/drug/{fileName:.+}")
    public ResponseEntity drugPhoto(@PathVariable("fileName") String fileName) {
        try {
            return ResponseEntity.ok().body(resourceLoader.getResource("file:" + Paths.get(ApplicationUtil.getDrugPath().getAbsolutePath(), fileName).toString()));
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }

}
